package one.stand.service.user;

import lombok.extern.slf4j.Slf4j;
import one.stand.dto.UserDto;
import one.stand.mapper.UserLoginWxMapperExt;
import one.stand.mapper.UserMapperExt;
import one.stand.mapper.UserPublicMapper;
import one.stand.model.User;
import one.stand.model.UserLoginWx;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author cjr
 */
@Service
@Slf4j
public class UserAdminServiceImpl implements UserAdminService {

    @Autowired
    private UserPublicMapper userPublicMapper;
    @Autowired
    private UserMapperExt userMapperExt;
    @Autowired
    private UserLoginWxMapperExt userLoginWxMapperExt;

    @Override
    @Transactional
    public void updateUserRoleId(Integer userId, Integer roleId) {
        User user = new User();
        user.setUserId(userId);
        user.setRoleId(roleId);
        userMapperExt.updateByPrimaryKeySelective(user);
    }

    @Override
    @Transactional
    public void updatePhoneGzh(Integer userId, String phone) {
        User user = getByUserId(userId);

        User userModel = new User();
        userModel.setUserId(userId);
        userModel.setPhone(phone);
        if (UserAdminService.GZH_STATE_GZH == user.getGzhState()) {
            userModel.setGzhState(UserAdminService.GZH_STATE_PHONE);
        }
        userMapperExt.updateByPrimaryKeySelective(userModel);
    }

    @Override
    public User getByUserId(Integer userId) {
        User user = userMapperExt.selectByPrimaryKey(userId);
        if (Objects.nonNull(user) && user.getIsDel() == 0) {
            user.setNickeName(decode(user.getNickeName()));
            return user;
        }
        return null;
    }

    @Override
    public boolean isVisit(String avatarUrl) {
        return StringUtils.equals(UserAdminServiceImpl.DEFAULT_IMG, avatarUrl);
    }

    @Override
    public String decode(String nickName) {
        if (StringUtils.isBlank(nickName)) return StringUtils.EMPTY;
        return new String(java.util.Base64.getDecoder().decode(nickName), StandardCharsets.UTF_8);
    }

    @Override
    public String getOpenId(Integer userId, Integer clientType, String companyKey) {
        UserLoginWx userLoginWx = userLoginWxMapperExt.select(userId, companyKey, clientType);
        return Objects.nonNull(userLoginWx) ? userLoginWx.getOpenId() : null;
    }

    @Override
    public UserDto get(Integer userId) {
        if (Objects.nonNull(userId) && userId < 800) {//admin
            UserDto admin = userPublicMapper.selectAdmin(userId);
            if (Objects.nonNull(admin)) {
                return UserDto.builder()
                        .userId(userId)
                        .adminUser(true)
                        .name(admin.getName())
                        .loginName(admin.getLoginName())
                        .sex(admin.getSex())
                        .position(admin.getPosition())
                        .tourist(admin.getTourist())
                        .build();
            }
        } else if (Objects.nonNull(userId)) {//user
            User user = getByUserId(userId);
            if (Objects.nonNull(user)) {
                return UserDto.builder()
                        .userId(userId)
                        .adminUser(false)
                        .name(user.getNickeName())
                        .userImg(user.getAvatarUrl())
                        .phone(user.getPhone())
                        .unionid(user.getUnionid())
                        .roleId(user.getRoleId())
                        .province(user.getProvince())
                        .build();
            }
        }
        return UserDto.empty(userId);
    }

}
